package com.sgcc.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.sgcc.common.core.domain.R;
import com.sgcc.system.api.domain.SysArea;
import com.sgcc.system.api.domain.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.system.service.ISysAreaService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;

/**
 * 区域Controller
 * 
 * @author sgcc
 * @date 2021-05-08
 */
@RestController
@RequestMapping("/area")
public class SysAreaController extends BaseController
{
    @Autowired
    private ISysAreaService sysAreaService;


    @RequestMapping("/getAreaDict")
    public List<SysDictData> getAreaDict()
    {   List<SysDictData> dicts=new ArrayList<SysDictData>();
        List<SysArea> list = sysAreaService.getAreaAll();
        for (SysArea arsa:list ) {
            SysDictData data=new SysDictData();
            data.setDictValue(arsa.getCode());
            data.setDictLabel(arsa.getName());
            dicts.add(data);
        }
        return dicts;
    }
    @RequestMapping("/getAreaBycode")
    public SysArea getAreaBycode(String code)
    {
        SysArea area = sysAreaService.getAreaBycode(code);
        return area;
    }
    /**
     * 查询区域列表
     */
    //@PreAuthorize(hasPermi = "system:area:list")
    @GetMapping("/list")
    public AjaxResult list(SysArea sysArea)
    {
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        return AjaxResult.success(list);
    }

    /**
     * 导出区域列表
     */
    //@PreAuthorize(hasPermi = "system:area:export")
    @Log(title = "区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysArea sysArea) throws IOException
    {
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        ExcelUtil<SysArea> util = new ExcelUtil<SysArea>(SysArea.class);
        util.exportExcel(response, list, "area");
    }

    /**
     * 获取区域详细信息
     */
    //@PreAuthorize(hasPermi = "system:area:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(sysAreaService.selectSysAreaById(id));
    }

    /**
     * 新增区域
     */
    //@PreAuthorize(hasPermi = "system:area:add")
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysArea sysArea)
    {
        return toAjax(sysAreaService.insertSysArea(sysArea));
    }

    /**
     * 修改区域
     */
    //@PreAuthorize(hasPermi = "system:area:edit")
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysArea sysArea)
    {
        return toAjax(sysAreaService.updateSysArea(sysArea));
    }

    /**
     * 删除区域
     */
    //@PreAuthorize(hasPermi = "system:area:remove")
    @Log(title = "区域", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sysAreaService.deleteSysAreaByIds(ids));
    }


    @RequestMapping("/getArealistBycode")
    public List<SysArea> getArealistBycode(List<String> orgList)
    {
        List<SysArea> list = sysAreaService.getArealistBycode(orgList);
        return list;
    }

    @GetMapping("/getAreaByDeptId/{deptId}")
    public R<SysArea> getAreaByDeptId(@PathVariable String deptId)
    {
        SysArea sysArea = sysAreaService.getAreaByDeptId(deptId);
        return R.ok(sysArea);
    }
}
